<template>
  <el-upload
    action="/api/storages"
    list-type="picture-card"
    :on-preview="handlePictureCardPreview"
    :on-remove="handleRemove"
    :headers="header"
    :on-success="handelSuccess"
    :file-list="fileList"
    name="multipartFile"
  >
    <el-icon><plus /></el-icon>
  </el-upload>
  <el-dialog v-model="dialogVisible">
    <img :src="dialogImageUrl" alt="" />
  </el-dialog>
</template>

<script>
import { Plus } from "@element-plus/icons";
import Cookies from "js-cookie";
export default {
  props: {
    imgVal: {
      type: String,
      default: "",
    },
  },
  name: "MyUpload",
  components: {
    Plus,
  },
  data() {
    return {
      //  缩略图地址
      dialogImageUrl: "",
      dialogVisible: false,
      header: { Authorization: Cookies.get("token") },
      imgUrl: "",
      fileList: [],
    };
  },
  mounted() {},
  methods: {
    handlePictureCardPreview(file) {
      this.dialogVisible = true;
      this.dialogImageUrl = file.url;
      console.log(this.fileList);
    },
    handelSuccess(file) {
      this.imgUrl = file.data.url;
      this.fileList.push({ url: file.data.url });
      this.$emit("getImg", this.imgUrl);
    },
    handleRemove(file) {
      console.log(file);
    },
  },
};
</script>

<style scoped></style>
